using System;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using E4RPManager.DataSets;

namespace E4RPManager.Controls
{
    public partial class ClientsXtraUserControl : XtraUserControl
    {
        private Controller _controller;
        
        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.ClientSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public ClientsXtraUserControl()
        {
            InitializeComponent();
            Load += ClientsXtraUserControlLoad;
            gridView.DoubleClick += GridViewDoubleClick;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            var dataSet = _controller.GetClients();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Cliente.TableName;             
        }

        private void SetupOperatorLookupData()
        {
            //operadoraDataSet.Operadora.AddOperadoraRow(-1, "");
            //operadoraDataSet.Merge(_controller.GetOperators());
            //operatorRepositoryItemLookUpEdit.Columns.Clear();
            //operatorRepositoryItemLookUpEdit.Columns.AddRange(new[] {
            //    new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
            //    new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            //operatorRepositoryItemLookUpEdit.DataSource = operadoraDataSet.Operadora;
            //operatorRepositoryItemLookUpEdit.DisplayMember = operadoraDataSet.Operadora.NomeColumn.ColumnName;
            //operatorRepositoryItemLookUpEdit.ValueMember = operadoraDataSet.Operadora.IdColumn.ColumnName;
        }

        private void ShowClient(long documentId)
        {
            int handle = gridView.FocusedRowHandle;
            var window = new ClientXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
            if(documentId > 0)
                gridView.FocusedRowHandle = handle;            
        }

        #endregion

        public void NewClient()
        {
            ShowClient(-1);
        }

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "ClientsXtraUserControl"; }
        }


        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var client = (ClienteDataSet.ClienteRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (client == null)
                return;
            ShowClient(client.Id);
        }

        void ClientsXtraUserControlLoad(object sender, EventArgs e)
        {
            gridView.FocusedRowChanged += GridViewFocusedRowChanged;
            if (gridView.DataRowCount > 0)
                gridView.FocusedRowHandle = 0;
        }

        void GridViewFocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //var group = (GrupoDataSet.GrupoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            //contactGridControl.DataSource = null;
            //if (group == null)
            //    return;
            //contactoDataSet = _controller.GetContactsByGroupId(group.Id);
            //contactGridControl.DataSource = null;
            //contactGridControl.DataSource = contactoDataSet;
            //contactGridControl.DataMember = contactoDataSet.Contacto.TableName;
        }

    }
}
